package 题集.牛客Top101.CH2_二分查看.BM17_二分查找;

import java.util.Arrays;

/**
 * 升序数组的二分法查找
 *
 * @see https://www.nowcoder.com/practice/d3df40bd23594118b57554129cadf47b
 */
public class Solution {
    public int search(int[] nums, int target) {
        int length = nums.length;

        if(nums.length<1){
            return -1;
        } else if (length==1) {
            return target==nums[0]?nums[0]:-1;
        }

        int res = -1;

        int center = nums[(length/2)-1];

        if (target < center) {
            res = search(Arrays.copyOfRange(nums, 0, (length / 2) - 2), target); //+新数组长度的1/2
        }else if(target > center) {
            res = search(Arrays.copyOfRange(nums, (length / 2), length), target);
        }else {
            res = (length/2)-1;
        }

        return res;//加上count * length+1
    }

    public static void main(String[] args) {
        System.out.println(new Solution().search(new int[]{-1, 0, 3, 4,    6, 10, 13, 14}, 13));
    }
}